package main

func minSum(nums1 []int, nums2 []int) int64 {
    get := func(nums []int) (int, int) {
		s, c := 0, 0
		for _, v := range nums {
			s += v
			if v == 0 {
				c++
			}
		}
		return s, c
	}
	s1, c1 := get(nums1)
	s2, c2 := get(nums2)
	if s1 == s2 {
		if c1 == 0 || c2 == 0 {
			return -1
		}
		return int64(s1 + max(c1, c2))
	} else {
		a, b := s1, c1
		c, d := s2, c2
		if s2 > s1 {
			a, b = s2, c2
			c, d = s1, c1
		}
		if d == 0 || (b == 0 && c > a - c) {
			return -1
		}
		return int64(a + b)
	}
}